<?php

namespace app\api\model;


use AlibabaCloud\SDK\Ocrapi\V20210707\Ocrapi;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;

use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Ocrapi\V20210707\Models\RecognizeEduQuestionOcrRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;

class Ocr
{

    public static function ceshi($img)
    {
        // 配置你的AccessKey信息
        $accessKeyId = 'LTAI5tSMb5H9s5e6VbZ2eW6r';
        $accessKeySecret = '4QpiGuOx1i0U9qultqcIWlwIE6QIUJ';

        // 上传图片文件路径
        //$imageFilePath = 'https://cuotiben.xing-peng.com/uploads/20250409/11c01a47f342623f1997f993ab1a4bfd.png';
        $host = "https://mquestion.market.alicloudapi.com";
        $path = "/ocrservice/mathQuestion";
        $method = "POST";
        $appcode = "ad4abeaa3da6451b9b9fe511b0ece2f4";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        //根据API的要求，定义相对应的Content-Type
        array_push($headers, "Content-Type".":"."application/json; charset=UTF-8");
        $querys = "";
        //$bodys = "{"img": "",}";
        $bodys = [
            "url"=>$img,
            //"prob"=>true,
            //'rotate'=>true
        ];
        //dump($bodys);
        $bodys = json_encode($bodys,320);
        $url = $host . $path;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
        $res = curl_exec($curl);
        /*$data = [
            'data'=>  json_decode($res,true),
            'bodys'=>$bodys
        ];
        return $data;*/
        return json_decode($res,true);
        //var_dump($res);
        //die;
    }

    // 计算签名
    public static function computeSignature($accessKeySecret, $method, $path, $query, $body) {
        $query = http_build_query($query);
        $stringToSign = strtoupper($method) . "\n" .
            $path . "\n" .
            $query . "\n" .
            $body;
        $signature = base64_encode(hash_hmac('sha1', $stringToSign, $accessKeySecret, true));
        return $signature;
    }


    /**
     * 使用AK&SK初始化账号Client
     * @return Ocrapi Client
     */
//    public static function createClient(){
//        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
//        // 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/311677.html。
//        $config = new Config([
//            // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
//            "accessKeyId" => 'LTAI5tSMb5H9s5e6VbZ2eW6r',
//            // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
//            "accessKeySecret" => '4QpiGuOx1i0U9qultqcIWlwIE6QIUJ'
//        ]);
//        // Endpoint 请参考 https://api.aliyun.com/product/ocr-api
//        $config->endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
//        return new Ocrapi($config);
//    }
//
//    /**
//     * @param string[] $args
//     * @return void
//     */
//    public static function main($args){
//
//        $client = self::createClient();
//        $recognizeEduQuestionOcrRequest = new RecognizeEduQuestionOcrRequest([]);
//        $runtime = new RuntimeOptions([]);
//        try {
//            // 复制代码运行请自行打印 API 的返回值
//            $client->recognizeEduQuestionOcrWithOptions($recognizeEduQuestionOcrRequest, $runtime);
//        }
//        catch (Exception $error) {
//            if (!($error instanceof TeaError)) {
//                $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
//            }
//            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
//            // 错误 message
//            var_dump($error->message);
//            // 诊断地址
//            var_dump($error->data["Recommend"]);
//            Utils::assertAsString($error->message);
//        }
//
//    }
}